/*
 * @Author: jqgao2
 * @Date: 2022-10-25 10:11:54
 * @LastEditTime: 2022-10-25 10:13:24
 * @Description: 通用工具
 * @FilePath: \vue2tem\src\utils\common.js
 * @LastEditors: jqgao2
 */

// 二进制文件下载
export function downFile(blob, fileName) {
  if (window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, fileName);
  } else {
    let link = document.createElement("a");
    link.href = window.URL.createObjectURL(blob);
    link.download = fileName;
    link.click();
    window.URL.revokeObjectURL(link.href);
  }
}

// 新标签页打开链接
export function openUrlNewTab(url) {
  console.log("open new tab with:", url);
  window.open(url, "_blank");
}

//函数节流
export const throttle = (func, delay = 500) => {
  let prev = 0;
  return function () {
    let now = Date.now();
    if (now - prev >= delay) {
      func.apply(this, arguments);
      prev = Date.now();
    }
  };
};

// 函数防抖
export const debounce = (fn, delay = 500) => {
  let timer;
  return function () {
    clearTimeout(timer);
    timer = setTimeout(() => {
      // 改变this指向并传递参数
      fn.apply(this, arguments);
    }, delay);
  };
};
